package com.gopay.scheduler.dao.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.trans.GatewaySource;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.order.impl.CpsGeneralMainOrderQueryDAOImpl;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.scheduler.dao.CpsGenMainOrderReadDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository("cpsGenMainOrderReadDao")
public class CpsGenMainOrderReadDaoImpl extends BaseDAOImpl<CpsGenMainOrder, String> implements CpsGenMainOrderReadDao {

	private Logger logger = LoggerFactory.getLogger(CpsGeneralMainOrderQueryDAOImpl.class);
	
	@Override
	public List<CpsGenMainOrder> findTimeoutOrdersByCreateTimeAndProcessCode(String txnStaCd) {
		String hql = " from CpsGenMainOrder t where  1=1 AND txnStaCd = :txnStaCd AND gatewaySource in (:gatewaySource) AND "
				   + " (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm + 1, 'yyyymmddhh24miss')) > 0 ";
//					+ " (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm + 1/720, 'yyyymmddhh24miss')) > 0 ";//fordebug 20170705
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("txnStaCd", Integer.parseInt(txnStaCd));
		String[] gatewaySource={GatewaySource.GATEWAY_SOURCE_05.value,GatewaySource.GATEWAY_SOURCE_08.value} ;
		param.put("gatewaySource",gatewaySource);
		logger.info("findTimeoutOrdersByCreateTimeAndProcessCode sql: " + hql + ",txnStaCd="+Integer.parseInt(txnStaCd)+",gatewaySource="+GatewaySource.GATEWAY_SOURCE_05.value);
		List<CpsGenMainOrder> list = findByHql(hql, param);
		return list;
	}
	
	@Override
	public List<CpsGenMainOrder>  findTimeoutBounsOrdersByCreateTime(String txnStaCd){
		String hql = " from CpsGenMainOrder t where  1=1  AND gopayOrderId in (select gopayOrderId from WaBounsMainOrder m where bounsStaCd =:txnStaCd) AND "
				   + " (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm + 1, 'yyyymmddhh24miss')) > 0 ";
//					+ " (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm + 1/720, 'yyyymmddhh24miss')) > 0 ";//fordebug 20170705
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("txnStaCd", Integer.parseInt(txnStaCd));
		logger.info("findTimeoutBounsOrdersByCreateTime sql: " + hql + ",txnStaCd="+txnStaCd);
		List<CpsGenMainOrder> list = findByHql(hql, param);
		return list;
	}
	
	 @Override
     public List<CpsGenMainOrder> findTimeoutTyyOrdersByCreateTimeAndProcessCode(String txnStaCd) {
         StringBuffer sb=new StringBuffer();
         sb.append(" from CpsGenMainOrder ");
         sb.append(" where  1=1 ");
         sb.append(" AND txnStaCd =:txnStaCd ");
         sb.append(" AND gatewaySource in (:gatewaySource) ");
         sb.append(" AND gopayIntTxnCd =:gopayIntTxnCd ");
         sb.append(" AND (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm +1/48 , 'yyyymmddhh24miss')) > 0 "); 
//         sb.append(" AND (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm +1/144 , 'yyyymmddhh24miss')) > 0 ");
         Map<String, Object> param = new HashMap<String, Object>();
         param.put("txnStaCd", Integer.parseInt(txnStaCd));
         List<String> gatewaySourceList = new ArrayList<String>();
         gatewaySourceList.add(GatewaySource.GATEWAY_SOURCE_05.value);
         gatewaySourceList.add(GatewaySource.GATEWAY_SOURCE_20.value);//增加兜兜第三方购买的tyy add by zhoupeng 20171025
         param.put("gatewaySource",gatewaySourceList.toArray());
         param.put("gopayIntTxnCd",IntTxnCd._01615.value);
         logger.info("updateTyyMainOrderStatusJob sql: "+sb.toString());
         List<CpsGenMainOrder> list = findByHql(sb.toString(), param);
         return list;
     }
     @Override
     public List<CpsGenMainOrder> findBankTimeoutTyyOrdersByCreateTimeAndProcessCode(List<Integer> txnStaCodeList) {
         StringBuffer sb=new StringBuffer();
         sb.append(" from CpsGenMainOrder ");
         sb.append(" where  1=1 ");
         sb.append(" AND txnStaCd in (:txnStaCds) ");
         sb.append(" AND gatewaySource in (:gatewaySource) ");
         sb.append(" AND gopayIntTxnCd =:gopayIntTxnCd ");
         sb.append(" AND  (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm + 1, 'yyyymmddhh24miss')) > 0  "); 
         //sb.append(" AND  (TO_CHAR(systimestamp, 'yyyymmddhh24miss')-TO_CHAR(gopayTxnTm + 1/144, 'yyyymmddhh24miss')) > 0  "); 
         Map<String, Object> param = new HashMap<String, Object>();
         param.put("txnStaCds", txnStaCodeList.toArray());
         List<String> gatewaySourceList = new ArrayList<String>();
         gatewaySourceList.add(GatewaySource.GATEWAY_SOURCE_05.value);
         gatewaySourceList.add(GatewaySource.GATEWAY_SOURCE_20.value);//增加兜兜第三方购买的tyy add by zhoupeng 20171025
         param.put("gatewaySource",gatewaySourceList.toArray());
         param.put("gopayIntTxnCd",IntTxnCd._01615.value);
         logger.info("updateTyyMainOrderStatusJob sql: "+sb.toString());
         List<CpsGenMainOrder> list = findByHql(sb.toString(), param);
         return list;
     }
}
